Ext.define('Admin.controller.DetailController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.detail',

    requires: [
        'Ext.Toast'
    ],
    config: {
        control: {
            'button[text=插入]': {
                click: 'onInsert'
            }
        }
    },
    validForms:function(){
     	var forms = this.getView().query('form');
       	for(var i=0; i<forms.length; i++){
               if(!forms[i].isValid()) {
               	return false;
               }
       	}
       	return true;
    },
	onMore:function(btn,e){
		var vw = this.getView();
		var ct = vw.items.items[0];
		if(btn.getText().indexOf('>>>')!=-1){
			if(!this.mitems){
				ct.on('add',this.refreshNav,this);
				ct.on('remove',this.refreshNav,this);
				this.mitems=ct.add(vw.moreItems);			
			}else{
				ct.add(this.mitems);
			}			
			btn.setText('<<<隐藏更多');
		}else{
			for(var i=0; i<this.mitems.length; i++){
				ct.remove(this.mitems[i],false);
			}			
			btn.setText('>>>显示更多');
		}
	},
	refreshNav:function(){
		var me=this;
		if(me.timer_add){
			clearTimeout(me.timer_add);
		}
		me.timer_add = setTimeout(function(){
			me.getView().refreshNav();
		},300);
	},
    onInsert:function(btn,e){
    	//return;
    	var vw = this.getView();
    	var he = vw.down('htmleditor');
    	var dv = btn.up('panel').down('dataview');
    	var sels = dv.getSelectionModel().getSelection();
    	var text="";
    	for(var i=0; i<sels.length; i++){
    		var url = sels[i].get('uri');
    		if(url && url.indexOf('.jpg')!=-1 || url.indexOf('.png')!=-1)
    			text+='<img style="float:left;margin:5px;" src="'+url+'"/>';
    		else
    			text+='<a href="'+url+'">'+ sels[i].get('name')+'</a>';
    	}
    	he.insertAtCursor(text);
    	he.focus();
    },
    onBackClick: function() {
		//Ext.util.History.back();
    	var rec = this.getViewModel().get('theObj');
    	rec.reject(true);
    	var nd = this.getView().xtype.replace('detail','list');
    	this.redirectTo(nd);		
    },    
    onSaveClick: function() {
		//TODO 6.2版本form缺validate, 6.5具备
/*    	var forms = this.getView().query('formpanel');
    	for(var i=0; i<forms.length; i++){
            if(!forms[i].isValid()) {
            	return false;
            }
		}*/
        var rec = this.getViewModel().get('theObj');
        rec.save({
            scope: this,
            callback: this.onComplete
        });
    },

    onComplete: function(records,operation,success) {
    	if(!success)
    		return;
    	var vw = this.getView();
    	var title = vw.getTitle();
        //Ext.Msg.hide();
        Ext.toast({
            message: title+'保存完成'
        });
        //判断,有返回按钮才返回
        var btn_return = vw.down('button[text=取消]');
        if(!btn_return || !btn_return.isHidden())
        	this.onBackClick();
        else{
        	vw.up('window').close();
        }
    },
    //获得仿真数据总数
    getEntityCount:function(){
    	var vm = this.getViewModel();
    	var cn  = Ext.getClassName(vm.data.theObj);
    	var pos=cn.lastIndexOf('.');
    	return DCApp.Cfg.SIM_ENM[cn.substring(pos+1)];
    },
    onNext: function() {
  	 	this.fireViewEvent('objnext');    	 	
   },
    onPrev: function() {
		this.fireViewEvent('objprev');    	
    	
    }
});
